define(function () {
    'use strict';

    function ctrl($scope,ConcernService,CommonService,InfoService) {
        $scope.vm = {};
        $scope.page_no = 1;
        $scope.page_size = 20;
        $scope.init = false;
        $scope.vm.showOther=false;
        init();
        $scope.$on('$ionicView.afterEnter', function() {
            $scope.vm.loadMore = true;
        });


        function init(){
            $scope.userCareFunds = new Array();
            $scope.otherFunds = new Array();
            $scope.items = new Array();
            //用户关注列表
            userCareList();
            //获取推荐关注列表
            othersCareList();
        }

        $scope.loadMore = function() {
            if($scope.page_no*$scope.page_size>=$scope.concernSize){
                $scope.items = $scope.items.concat($scope.userCareFunds.slice(($scope.page_no-1)*$scope.page_size,$scope.userCareFunds.length));
            }else{
                $scope.items = $scope.items.concat($scope.userCareFunds.slice(($scope.page_no-1)*$scope.page_size,$scope.page_no*$scope.page_size));
            }
            if($scope.page_no==$scope.total_no){
                $scope.vm.loadMore = false;
                $scope.vm.showOther=true;
            }else{
                $scope.page_no++;
            }
            perfectFunds($scope.items);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        }

        function userCareList(){
            var prodCodes= ConcernService.getConcernList();
            $scope.concernSize = prodCodes.length;
            if(prodCodes != "") {
                InfoService.getProdBriefs(prodCodes).then(
                    function(data){
                        if(data){
                            $scope.userCareFunds = data;
                            $scope.concernSize = $scope.userCareFunds.length;
                            $scope.init = true;
                            $scope.total_no = Math.ceil($scope.concernSize/$scope.page_size);
                        }
                    }
                );
            }else{
                $scope.vm.showOther=true;
            }
        }

        function othersCareList(){
            InfoService.getOthersCare().then(
                function(data){
                    if(data){
                        $scope.otherFunds = data;
                        perfectFunds( $scope.otherFunds);
                    }
                }
            );
        }

        $scope.unCare = function(prod_code){
            ConcernService.concern(prod_code);
            var list = $scope.items;
            for(var i=0;i<list.length;i++){
                if(prod_code == list[i].prod_code){
                    list.splice(i,1);
                    $scope.concernSize = list.length;
                }
            }
        };

        function perfectFunds(funds){
            for(var i=0;i < funds.length; i++) {
                var fund = funds[i];
                fund.url = CommonService.productPageUrl(fund.prod_profit_mode, fund.prod_source, fund.prod_catalog_mark);
            }
        }
    }

    ctrl.$inject = ['$scope','ConcernService','CommonService','InfoService'];
    return ctrl;
});